package com.ruoyi.web.controller.business;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.business.domain.TAliZftMerchantInfo;
import com.ruoyi.business.domain.TIsvInfo;
import com.ruoyi.business.domain.TUserChannel;
import com.ruoyi.business.service.AliZftPayToolService;
import com.ruoyi.business.service.ITAliZftMerchantInfoService;
import com.ruoyi.business.service.ITIsvInfoService;
import com.ruoyi.business.service.ITUserChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直付通二级商户进件Controller
 * 
 * @author ruoyi
 * @date 2024-04-01
 */
@Controller
@RequestMapping("/business/aliZftMerchantInfo")
public class TAliZftMerchantInfoController extends BaseController
{
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private String prefix = "business/aliZftMerchantInfo";

    @Autowired
    private ITAliZftMerchantInfoService tAliZftMerchantInfoService;

    @Autowired
    private ITUserChannelService userChannelService;

    @Autowired
    private AliZftPayToolService aliZftPayToolService;

    @Autowired
    private ITIsvInfoService itIsvInfoService;


    @RequiresPermissions("business:aliZftMerchantInfo:view")
    @GetMapping()
    public String aliZftMerchantInfo()
    {
        return prefix + "/aliZftMerchantInfo";
    }

    /**
     * 查询直付通二级商户进件列表
     */
    @RequiresPermissions("business:aliZftMerchantInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TAliZftMerchantInfo tAliZftMerchantInfo)
    {
        startPage();
        List<TAliZftMerchantInfo> list = tAliZftMerchantInfoService.selectTAliZftMerchantInfoList(tAliZftMerchantInfo);
        return getDataTable(list);
    }

    /**
     * 导出直付通二级商户进件列表
     */
    @RequiresPermissions("business:aliZftMerchantInfo:export")
    @Log(title = "直付通二级商户进件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TAliZftMerchantInfo tAliZftMerchantInfo)
    {
        List<TAliZftMerchantInfo> list = tAliZftMerchantInfoService.selectTAliZftMerchantInfoList(tAliZftMerchantInfo);
        ExcelUtil<TAliZftMerchantInfo> util = new ExcelUtil<TAliZftMerchantInfo>(TAliZftMerchantInfo.class);
        return util.exportExcel(list, "直付通二级商户进件数据");
    }

    /**
     * 新增直付通二级商户进件
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        TIsvInfo query = new TIsvInfo();
        query.setIsvType(0);
        mmap.put("qryIsvInfo",query);
        return prefix + "/add";
    }

    /**
     * 新增保存直付通二级商户进件
     */
    @RequiresPermissions("business:aliZftMerchantInfo:add")
    @Log(title = "直付通二级商户进件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addSave(TAliZftMerchantInfo tAliZftMerchantInfo) throws Exception {
        SysUser sysUser =  ShiroUtils.getSysUser();
        if(sysUser.getMerchantType().equals("2")){
            return error("您无权限进件");
        }
        TUserChannel userChannel = new TUserChannel();
        userChannel.setUserId(sysUser.getUserId());
        userChannel.setChannel("isv_alipay_zft");
        List<TUserChannel> userChannelList = userChannelService.selectTUserChannelList(userChannel);
        if(userChannelList.size()!=1){
            return error("未配置直付通通道");
        }
//        TIsvInfo isvInfo = new TIsvInfo();
//        isvInfo.setIsvType(0);
//        List<TIsvInfo> isvInfoList = itIsvInfoService.selectTIsvInfoList(isvInfo);
//        if(isvInfoList.size()==0){
//            return error("未配置直付通通道");
//        }
//        userChannel = userChannelList.get(0);
//        if(StringUtils.isBlank(userChannel.getIsvInfo())){
//            return error("未配置直付通主体信息");
//        }
        TAliZftMerchantInfo query = new TAliZftMerchantInfo();
//        query.setIsvId(tAliZftMerchantInfo.getIsvId());
        query.setDefaultSettleTarget(tAliZftMerchantInfo.getAlipayLogonId());
        List<TAliZftMerchantInfo> tAliZftMerchantInfoList = tAliZftMerchantInfoService.selectTAliZftMerchantInfoList(query);
        if(tAliZftMerchantInfoList.size()>0){
            return error("该二级商户已经添加，请查看是否进件成功！");
        }
        tAliZftMerchantInfo.setUserId(sysUser.getUserId());
//        tAliZftMerchantInfo.setIsvId(userChannel.getIsvInfo());
        tAliZftMerchantInfo.setDefaultSettleTarget(tAliZftMerchantInfo.getAlipayLogonId());
        String external_id = RandomUtil.randomNumbers(16);
        tAliZftMerchantInfo.setExternalId(external_id);
        tAliZftMerchantInfoService.insertTAliZftMerchantInfo(tAliZftMerchantInfo);
        //调用进件接口
        aliZftPayToolService.AntMerchantExpandIndirectZftSimplecreate(tAliZftMerchantInfo);
        return toAjax(1);
    }

    /**
     * 修改直付通二级商户进件
     */
    @RequiresPermissions("business:aliZftMerchantInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TAliZftMerchantInfo tAliZftMerchantInfo = tAliZftMerchantInfoService.selectTAliZftMerchantInfoById(id);
        mmap.put("tAliZftMerchantInfo", tAliZftMerchantInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存直付通二级商户进件
     */
    @RequiresPermissions("business:aliZftMerchantInfo:edit")
    @Log(title = "直付通二级商户进件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TAliZftMerchantInfo tAliZftMerchantInfo)
    {
        return toAjax(tAliZftMerchantInfoService.updateTAliZftMerchantInfo(tAliZftMerchantInfo));
    }

    /**
     * 删除直付通二级商户进件
     */
    @RequiresPermissions("business:aliZftMerchantInfo:remove")
    @Log(title = "直付通二级商户进件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tAliZftMerchantInfoService.deleteTAliZftMerchantInfoByIds(ids));
    }
}
